// Author: wsfuyibing <682805@qq.com>
// Date: 2025-07-02

package session

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/http/logics/user/coupon/visible"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib/authorizer"
)

// Logic
// 优惠券列表.
type Logic struct {
	auth     *authorizer.Authentication
	request  *visible.Request
	response []*visible.Response
}

func New() logics.Logical {
	return &Logic{
		request:  &visible.Request{},
		response: make([]*visible.Response, 0),
	}
}

func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Query(i, o.request); err == nil {
		o.auth = authorizer.New().Get(i)
	}
	return
}

func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err  error
		list []*models.Coupon
		res  = result.New()
	)
	if list, err = services.NewCouponService().LatestVisible(ctx, o.request.ArcadeId, o.auth.UserId, kind.OrderKindSession); err != nil {
		return res.WithError(err)
	}
	for _, item := range list {
		o.response = append(o.response, (&visible.Response{}).With(item))
	}
	return res.WithList(o.response)
}
